import pygtk
pygtk.require("2.0")
import gtk

import os.path as path
import sys
from jukee.utils.utils import debug, warn, error
from jukee.libs.Observable import Observer

class JukeeClientWindow(Observer):

  def __init__(self, event_listener):

    Observer.__init__(self)
    self.event_listener = event_listener
    self.gladefile = path.join(path.dirname(__file__), "jukee_client_gui.glade")
    debug("Loading glade file: " + self.gladefile)
    self.wTree = gtk.glade.XML(self.gladefile)
    
    self.window = self.wTree.get_widget("jukee_client_gui_window")
    if not self.window:
      error("Error in glade file. Aborting")
      sys.exit("Glade error")

    signals = {
      'on_add_song_button_clicked' : self.add_song,
      'on_comment_submit_clicked'  : self.submit_comment,
      'on_positive_button_clicked' : self.vote_positive,
      'on_negative_button_clicked' : self.vote_negative
    }

    self.wTree.signal_autoconnect(signals)
    self.window.connect("destroy", gtk.main_quit)
    self.observe(self.event_listener)

  def add_song(self, widget, data = None):
    warn("Add song: not implemented")

  def submit_comment(self, widget, data = None):
    comment_entry = self.wTree.get_widget("comment_entry")
    comment_entry.set_editable(False)
    buffer = comment_entry.get_buffer()
    _start, _end = buffer.get_bounds()
    debug("Comments content: " + comment_entry.get_buffer().get_text(_start, _end, False))
    comment_entry.set_editable(True)
    warn("Submit comment: not implemented")

  def vote_positive(self, widget, data = None):
    warn("Vote positive: not implemented")

  def vote_negative(self, widget, data = None):
    warn("Vot negative not implemented")

  def processEvent(self, notifier, event_name, *arguments):
    debug("Received event on GUI")
    debug(str(event_name) + " : " + str(arguments))

  def main(self):
    self.window.show()
    gtk.main()

